package org.rslai.autotest.core.page.impl;

import org.openqa.selenium.TimeoutException;
import org.rslai.autotest.core.page.AbstractHelper;
import org.rslai.autotest.core.page.Alert;
import org.rslai.autotest.core.page.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * html弹出对话框处理实现<br>
 * 包括：alert、confirm、prompt
 * @author rslai
 */
public class AlertImpl extends AbstractHelper implements Alert {
	@Autowired
	protected Assertions assertions;
	
	@Value("${webdriverwait.timeout}")
	private int webdriverwaitTimeout; // 使用wait查找对象的超时时间

	/**
	 * 单击confirm和prompt的确认按钮，并验证显示信息中包含指定内容
	 * @param containsMsg 包含的指定内容，为null则不判断是否包括指定信息
	 * @param answer prompt中输入的答案
	 * @return 显示的信息
	 */
	private String accept(String containsMsg, String answer) {
		this.waitAlertPresent();

		org.openqa.selenium.Alert alert = this.driver().switchTo().alert();
		if (answer != null) {
			alert.sendKeys(answer);
		}
		String alertStr = alert.getText();
		if (containsMsg != null) {
			this.assertions.assertContains(alertStr, containsMsg);
		}
		alert.accept();
		return alertStr;
	}

	/**
	 * 单击alert的确定、confirm和prompt的取消按钮，并验证显示信息中包含指定内容
	 * @param containsMsg 包含的指定内容，为null则不判断是否包括指定信息
	 * @return 显示的信息
	 */
	private String dismiss(String containsMsg) {
		this.waitAlertPresent();

		org.openqa.selenium.Alert alert = this.driver().switchTo().alert();
		String alertStr = alert.getText();
		if (!containsMsg.isEmpty()) {
			this.assertions.assertContains(alertStr, containsMsg);
		}
		alert.dismiss();
		return alertStr;
	}

	public String alertDismiss() {
		return this.dismiss(null);
	}

	public String alertDismiss(String containsMsg) {
		return this.dismiss(containsMsg);
	}

	public String confirmAccept() {
		return this.accept(null, null);
	}

	public String confirmAccept(String containsMsg) {
		return this.accept(containsMsg, null);
	}

	public String confirmDismiss() {
		return this.dismiss(null);
	}

	public String confirmDismiss(String containsMsg) {
		return this.dismiss(containsMsg);
	}

	public String promptAccept(String answer) {
		return this.accept("", answer);
	}

	public String promptAccept(String containsMsg, String answer) {
		return this.accept(containsMsg, answer);
	}

	public String promptDismiss() {
		return this.dismiss(null);
	}

	public String promptDismiss(String containsMsg) {
		return this.dismiss(containsMsg);
	}

	public boolean isAlertPresent() {
		Boolean b = true;
		try {
			this.driver().switchTo().alert();
		} catch (Exception e) {
			b = false;
		}
		return b;
	}

	public boolean isPromptPresent() {
		return isAlertPresent();
	}

	public boolean isConfirmPresent() {
		return isAlertPresent();
	}

	public boolean waitAlertPresent() {
		return this.waitAlertPresent(webdriverwaitTimeout);
	}

	public boolean waitAlertPresent(int waitTimeout) {
		for (int i=0; i<waitTimeout; i++) {
			if (this.isAlertPresent()) {
				return true;
			}
			super.sleep(1);
		}
		throw new TimeoutException("超时错误：经过" + waitTimeout + "秒后仍未等到alert（包括：alert、Confirm、Prompt）。");
	}

	public boolean waitConfirmPresent() {
		return this.waitConfirmPresent(webdriverwaitTimeout);
	}

	public boolean waitConfirmPresent(int waitTimeout) {
		return this.waitAlertPresent(waitTimeout);
	}

	public boolean waitPromptPresent() {
		return this.waitPromptPresent(webdriverwaitTimeout);
	}

	public boolean waitPromptPresent(int waitTimeout) {
		return this.waitAlertPresent(waitTimeout);
	}

}
